<template>
  <div class="box flex-center">
    <div class="login-box flex-center">
      <img src="../assets/images/login.png" alt="" />
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <el-form-item label="姓名" prop="username">
          <el-input v-model="ruleForm.username" @keyup.enter="submitForm(ruleFormRef)" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" @keyup.enter="submitForm(ruleFormRef)" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"
            >登录</el-button
          >
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
        <el-form-item>
          <p>账号：admin/editor</p>
        </el-form-item>
        <el-form-item>
          <p>密码：123456</p>
        </el-form-item>
      </el-form>
    </div>
    <Loading :loadShow='loadShow' v-if="loadShow"></Loading>
  </div>
</template>

<script lang="ts" setup>
import * as _ from 'lodash'
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { login } from "@/api/login";
import { ElMessage } from "element-plus";
import { loginStore } from "@/store/index";
import { submenuStore } from "@/store/submenu";
import router from "@/router";
const loadShow = ref(false)
const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  username: "admin",
  password: "123456",
});
const store = loginStore();
const menuStore = submenuStore();
const rules = reactive<FormRules>({
  username: [{ required: true, message: "请填写姓名", trigger: "blur" }],
  password: [{ required: true, message: "请填写密码", trigger: "blur" }],
});

const submitForm = _.throttle(async(formEl: FormInstance | undefined) => {
  if (!formEl) return;
  loadShow.value = true
  await formEl.validate((valid, fields) => {
    if (valid) {
      login(ruleForm)
        .then((res) => {
          const { code, data } = res.data;
          if (code != 200) {
            return ElMessage.warning("登录失败，请重新登录");
          }
          sessionStorage.setItem('auth',ruleForm.username);
          store.changeUserInfo({"auth": ruleForm.username});
          menuStore.setyjActive('0');
          store.setToken(data);
          loadShow.value = false
          router.push("/home");
        })
        .catch((err) => {
          console.log(err);
        });
    } else {
      loadShow.value = false
      ElMessage.warning("请填写完整！");
      console.log("error submit!", fields);
    }
  });
},1000);

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>
<style lang="scss" scoped>
@import '@/style/variables.module.scss';
.box {
  height: 100%;
  .login-box {
    background: #fff;
    padding: 20px;
    border-radius: 20px;
    max-width: 800px;
    img {
      width: 400px;
      width: 400px;
      border-right: 1px dashed $borderColor;
    }
    .el-form {
      display: flex;
      flex-wrap: wrap;
    }
  }
}
</style>
